package cn.cxj.quartz.jobs;

import cn.cxj.order.domain.AdoptOrder;
import cn.cxj.order.service.IAdoptOrderService;
import cn.cxj.pay.domain.PayBill;
import cn.cxj.pay.service.IPayBillService;
import cn.cxj.pet.domain.Pet;
import cn.cxj.pet.service.IPetService;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
public class MainJob implements Job {

    @Autowired
    private IPayBillService payBillService;

    @Autowired
    private IAdoptOrderService adoptOrderService;

    @Autowired
    private IPetService petService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        // 准备好查询参数
        JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        Map<String, Object> params = (Map<String, Object>) jobDataMap.get("params");
        System.out.println(params);
        String orderSn = (String) params.get("orderSn");
        // 根据订单号查询t_pay_bill表,修改状态为未支付和更新修改时间
        PayBill payBill = payBillService.queryByOrderSn(orderSn);
        payBill.setState(-1);
        payBill.setUpdateTime(new Date());
        payBillService.update(payBill);
        // 再修改领养订单的状态
        Long businessKey = payBill.getBusinessKey();
        AdoptOrder adoptOrder = adoptOrderService.queryById(businessKey);
        // 状态设置为未支付
        adoptOrder.setState(-1);
        adoptOrderService.update(adoptOrder);
        // 宠物重新上架
        Long petId = adoptOrder.getPet_id();
        Pet pet = petService.queryById(petId);
        pet.setState(1);
        petService.update(pet);
    }
}
